package com.amumu.drama.common.modules.order.manager;

import cn.hutool.core.util.StrUtil;
import com.amumu.drama.common.commpont.notify.OrderNotifyUtils;
import com.amumu.drama.common.constant.*;
import com.amumu.drama.common.exception.ApiException;
import com.amumu.drama.common.exception.Asserts;
import com.amumu.drama.common.modules.delivery.dto.DeliveryStatusResp;
import com.amumu.drama.common.modules.delivery.service.DeliveryService;
import com.amumu.drama.common.modules.member.manager.UmsMemberManager;
import com.amumu.drama.common.modules.order.dao.PortalOrderDao;
import com.amumu.drama.common.modules.order.dto.OmsOrderDetail;
import com.amumu.drama.common.modules.order.dto.resp.UserTranInfoDto;
import com.amumu.drama.common.modules.order.service.OmsPortalOrderService;
import com.amumu.drama.common.modules.service.RedisService;
import com.amumu.drama.common.modules.service.impl.payment.PaymentResp;
import com.amumu.drama.common.utils.MagicNumConstant;
import com.amumu.drama.mapper.OmsOrderItemMapper;
import com.amumu.drama.mapper.OmsOrderMapper;
import com.amumu.drama.mapper.OmsOrderOperateHistoryMapper;
import com.amumu.drama.mapper.UmsMemberMapper;
import com.amumu.drama.model.*;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author sunzhuang
 * @date 2024/5/13 14:26
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc todo 订单管理器
 */

@Service
@Slf4j
public class OrderManager {
    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private UmsMemberMapper umsMemberMapper;
    @Autowired
    private PortalOrderDao portalOrderDao;
    @Autowired
    private OmsOrderItemMapper orderItemMapper;
    @Autowired
    private OmsOrderOperateHistoryMapper orderOperateHistoryMapper;
    @Autowired
    private UmsMemberManager memberManager;

    @Value("${home.order.num:6574000}")
    private Long orderNum;

    @Value("${home.user.num:432000}")
    private Long userNum;

    @Value("${home.stat.button:true}")
    private Boolean isEnableStatConfig;

    @Autowired
    private RedisService redisService;

    @Autowired
    private OrderManager orderManager;

    @Autowired
    private OmsPortalOrderService omsPortalOrderService;

    @Autowired
    private DeliveryService deliveryService;

    private final static String[] BEFORE_MINUTE_STR = {"1分钟前", "5分钟前", "15分钟前", "20分钟前", "25分钟前", "30分钟前"};
    private final static String PRODUCT_NAME = "PUBG手游国际服-直充";
    private final static BigDecimal[] PRODUCT_AMOUNT = {new BigDecimal(1440), new BigDecimal(36), new BigDecimal(72), new BigDecimal(36), new BigDecimal(180), new BigDecimal(360)};
    private final static String[] PHONE_STR = {"15723458961", "15723458962", "15723458963", "15723458954", "15723458955", "15723458956"};

    /** 待支付变更支付中 */
    public Long getCodeCount(Long code) {
        return omsPortalOrderService.getCodeCount(code);
    }

//    /** 待支付变更支付中 */
//    public int payingByOrderId(Long orderId, String payOrderSn, String payRemark) {
//        return updateOrderPayStatus(orderId, null, payOrderSn, PayStatusEnum.PAYING, payRemark);
//    }
//
//    /** 支付成功  从支付中变更状态 */
//    public int paySuccessFromPayingByOrderId(Long orderId) {
//        return updateOrderPayStatus(orderId, null, null, PayStatusEnum.SUCCESS, null);
//    }
//
//    /** 支付失败  从支付中变更状态 */
//    public int payFailFromPayingByOrderId(Long orderId, String payRemark) {
//        return updateOrderPayStatus(orderId, null, null, PayStatusEnum.FAIL, payRemark);
//    }

    /** 订单发货，待发货变更发货中 */
//    public int updateDeliveryStatus(Long orderId, DeliveryStatusResp resp) {
//        return updateOrderDeliveryStatus(orderId, null, null, resp, OrderStatusEnum.WAIT_DELIVERY, OrderStatusEnum.DELIVERY);
//    }

    /** 订单发货成功 待发货变更已完成 */
//    public int deliverySuccess(Long orderId, DeliveryStatusResp resp) {
//        return updateOrderDeliveryStatus(orderId, null, null, resp, OrderStatusEnum.DELIVERY, OrderStatusEnum.FINISHED);
//    }

//    /** 支付成功 */
//    public int paySuccessByOrderId(Long orderId, String payOrderSn, String payRemark) {
//        return updateOrderPayStatus(orderId, null, payOrderSn, PayStatusEnum.SUCCESS, payRemark);
//    }
//
//    /** 支付失败 */
//    public int payFailByOrderId(Long orderId, String payOrderSn, String payRemark) {
//        return updateOrderPayStatus(orderId, null, payOrderSn, PayStatusEnum.FAIL, payRemark);
//    }

    /**
     * 更新订单状态
     *
     * @param orderId
     * @param paymentResp
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateOrderPayStatus(Long orderId, PaymentResp paymentResp, OperateTypeEnum operateTypeEnum, Long memberId) {
        if (paymentResp == null || paymentResp.getPayStatusEnum() == null) {
            Asserts.fail("fromStatus和toStatus不能为空");
        }
        PayStatusEnum toStatus = paymentResp.getPayStatusEnum();
        String payOrderSn = paymentResp.getPayOrderSn();
        OmsOrderDetail orderDetail = getOmsOrderDetailHighly(paymentResp.getOrderDetail(), orderId, paymentResp.getOrderSn(), payOrderSn);
        if (orderDetail == null) {
            log.error("OrderManager.updateOrderStatus订单不存在,无法进行操作,订单ID:{},toStatus:{}", orderId, toStatus);
            Asserts.fail("订单不存在，无法变更支付状态");
        }
        int resultCount = 0;
        PayStatusEnum fromStatus = PayStatusEnum.getByCode(orderDetail.getPayStatus());
        log.info("更新订单支付状态:fromStatus:{},toStatus:{}", fromStatus, toStatus);
        boolean paySuccess = (fromStatus == PayStatusEnum.WAIT_PAY || fromStatus == PayStatusEnum.PAYING) && toStatus == PayStatusEnum.SUCCESS;
        if (paySuccess) {
            //待支付->支付成功
            resultCount = paySuccess(paymentResp, orderDetail, payOrderSn);
        } else if ((fromStatus == PayStatusEnum.WAIT_PAY || fromStatus == PayStatusEnum.PAYING) && toStatus == PayStatusEnum.FAIL) {
            //待支付->支付失败
            resultCount = payFail(orderDetail, payOrderSn, paymentResp);
        } else if (fromStatus == PayStatusEnum.WAIT_PAY && toStatus == PayStatusEnum.PAYING) {
            //待支付->支付中
            resultCount = paying(paymentResp, orderDetail, payOrderSn);
        } else if (fromStatus == PayStatusEnum.SUCCESS && toStatus == PayStatusEnum.REFUNDING) {
            //已支付-->退款中
            resultCount = refunding(paymentResp, orderDetail, payOrderSn);
        } else if ((fromStatus == PayStatusEnum.REFUNDING || fromStatus == PayStatusEnum.SUCCESS) && toStatus == PayStatusEnum.REFUND) {
            //退款中-->已退款
            resultCount = refundSuccess(paymentResp, orderDetail, payOrderSn);
        } else if ((fromStatus == PayStatusEnum.REFUNDING || fromStatus == PayStatusEnum.SUCCESS) && toStatus == PayStatusEnum.FAIL) {
            //退款中-->退款失败
            resultCount = refundFail(orderDetail, payOrderSn, paymentResp);
        } else if (toStatus == PayStatusEnum.LOG) {
            resultCount = 1;
        }
        //记录订单操作日志
        if (resultCount > 0) {
            log.info("订单状态更新成功：orderId:{},status:{},reqInfo:{},respInfo:{},msg:{}", orderId, toStatus, paymentResp.getReqInfo(), paymentResp.getReqInfo(), paymentResp.getPayRemark());
            orderManager.saveOrderOperationLog(orderDetail, memberId, operateTypeEnum, OrderConstants.PAY_OPERATE_NAME,
                    paymentResp.getReqInfo(), paymentResp.getRespInfo());
            if (paySuccess) {
                try {
                    //接口调用发货
                    if (OrderTypeEnum.isInterface(orderDetail.getOrderType())) {
                        deliveryService.delivery(orderDetail.getId(), null, operateTypeEnum, memberId);
                    } else {
                        OrderNotifyUtils.manualOrderNotify(orderDetail);
                    }
                } catch (Exception e) {
                    log.error("支付成功，发货失败：", e);
                }
            }
        }
        return resultCount;
    }

    public OmsOrderDetail getOmsOrderDetailHighly(OmsOrderDetail orderDetail, Long orderId, String orderSn, String payOrderSn) {
        if (orderDetail == null && orderId != null) {
            orderDetail = portalOrderDao.getDetail(orderId);
        } else if (orderDetail == null && StrUtil.isNotBlank(payOrderSn)) {
            orderDetail = portalOrderDao.getDetailByPayOrderSn(payOrderSn);
        } else if (orderDetail == null && StrUtil.isNotBlank(orderSn)) {
            orderDetail = portalOrderDao.getDetailByOrderSn(orderSn);
        }
        return orderDetail;
    }

    /** 支付成功之后，更新订单状态 */
    public int updateOrderDeliveryStatus(Long orderId, OmsOrder omsOrder, DeliveryStatusResp resp, OperateTypeEnum operateTypeEnum, Long memberId) {
        omsOrder = getOmsOrderHighly(omsOrder, orderId, resp.getOrderSn(), resp.getDeliverySn());
        if (omsOrder == null) {
            log.error("OrderManager.updateOrderStatus订单不存在,无法变更订单状态,订单ID:{},orderSn:{},deliverySn:{}",
                    orderId, resp.getOrderSn(), resp.getDeliverySn());
            Asserts.fail("订单不存在，无法变更状态");
        }
        DeliveryStatusEnum fromStatus = DeliveryStatusEnum.getByCode(omsOrder.getDeliveryStatus());
        DeliveryStatusEnum toStatus = resp.getDeliveryStatusEnum();
        log.info("更新订单发货状态:fromStatus:{},toStatus:{}", fromStatus, toStatus);
        if (fromStatus == null || toStatus == null) {
            Asserts.fail("fromStatus和toStatus不能为空");
        }
        int resultCount = 0;
        if (!PayStatusEnum.SUCCESS.getCode().equals(omsOrder.getPayStatus())) {
            log.error("订单支付未成功，无法进行后续变更订单状态，orderId:{},orderPayStatus:{}", omsOrder.getId(), PayStatusEnum.getByCode(omsOrder.getPayStatus()));
            Asserts.fail("订单未支付成功，无法进行后续变更订单状态");
        }
        if (DeliveryStatusEnum.WAIT_DELIVERY.equals(fromStatus) && DeliveryStatusEnum.DELIVERY.equals(toStatus)) {
            // 订单发货 待发货->发货中
            resultCount = updateDeliveryStatus(omsOrder, toStatus, resp);
        } else if ((DeliveryStatusEnum.DELIVERY.equals(fromStatus) || DeliveryStatusEnum.WAIT_DELIVERY.equals(fromStatus))
                && DeliveryStatusEnum.SUCCESS.equals(toStatus)) {
            //订单发货 待发货->已完成
            resultCount = updateDeliveryStatus(omsOrder, toStatus, resp);
        } else if (DeliveryStatusEnum.FAIL.equals(toStatus)) {
            //订单发货 发货失败转换订单类型
            resultCount = orderManager.changeOrderType(omsOrder, resp, OrderTypeEnum.INTERFACE2MANUAL_DELIVERY, operateTypeEnum, memberId);
        } else {
            log.error("处理发货失败，没有对应的订单处理状态：orderId:[{}],fromStatus:[{}],toStatus:[{}},orderDetail:{}", orderId, fromStatus, toStatus, omsOrder);
        }
        // 发货状态变更 记录订单操作日志
        if (resultCount > 0) {
            //记录福禄发货日志
            log.info("订单发货状态更新成功：orderId:{},fromStatus:[{}],toStatus:[{}],reqInfo:{},respInfo:{},msg:{}", orderId, fromStatus, toStatus,
                    resp.getReqInfo(), resp.getRespInfo(), resp.getMsg());
            orderManager.saveOrderOperationLog(omsOrder, memberId == null ? omsOrder.getMemberId() : memberId, operateTypeEnum, OrderConstants.DELIVERY_OPERATE_NAME,
                    resp.getReqInfo(), resp.getRespInfo());
        }
        return 0;
    }

    public OmsOrder getOmsOrderHighly(OmsOrder omsOrder, Long orderId, String orderSn, String deliverySn) {
        if (omsOrder == null && orderId != null) {
            omsOrder = portalOrderDao.getDetail(orderId);
        } else if (omsOrder == null && StrUtil.isNotBlank(orderSn)) {
            omsOrder = portalOrderDao.getDetailByOrderSn(orderSn);
        } else if (omsOrder == null && StrUtil.isNotBlank(deliverySn)) {
            omsOrder = portalOrderDao.getDetailByDeliverySn(deliverySn);
        }
        return omsOrder;
    }

    private int updateDeliveryStatus(OmsOrder orderDetail, DeliveryStatusEnum toStatus, DeliveryStatusResp deliveryStatusResp) {
        OmsOrder order = new OmsOrder();
        order.setId(orderDetail.getId());
        switch (toStatus) {
            case WAIT_DELIVERY:
                order.setStatus(OrderStatusEnum.WAIT_DELIVERY.getCode());
                break;
            case SUCCESS:
                order.setStatus(OrderStatusEnum.FINISHED.getCode());
                break;
            case DELIVERY:
                order.setStatus(OrderStatusEnum.WAIT_DELIVERY.getCode());
                break;
            default:
                break;
        }
        order.setNote(deliveryStatusResp.getMsg());
        order.setDeliverySn(deliveryStatusResp.getDeliverySn());
        order.setDeliveryTime(deliveryStatusResp.getApplyTime() == null ? LocalDateTime.now() : deliveryStatusResp.getApplyTime());
        order.setDeliveryStatus(deliveryStatusResp.getDeliveryStatusEnum().getCode());
        order.setDeliveryRemark(deliveryStatusResp.getMsg());
        order.setReceiveTime(deliveryStatusResp.getApplyConfirmTime());
        order.setModifyTime(LocalDateTime.now());
        return orderMapper.updateById(order);
    }

    /**
     * 发货处理状态更新订单状态
     *
     * @param omsOrder
     * @param toOrderType
     */
    @Transactional(rollbackFor = Exception.class)
    public int changeOrderType(OmsOrder omsOrder, DeliveryStatusResp deliveryStatusResp, OrderTypeEnum toOrderType, OperateTypeEnum operateTypeEnum, Long operateId) {
        if (omsOrder == null || omsOrder.getId() == null) {
            log.error(" deliveryService#changeOrderType,order is null:[{}]", omsOrder);
            throw new ApiException("订单ID不能为空");
        }
        boolean manualNotify = false;
        OmsOrder order = new OmsOrder();
        order.setId(omsOrder.getId());
        order.setOrderType(toOrderType.getCode());
        if (OrderTypeEnum.isInterface(omsOrder.getOrderType()) && OrderTypeEnum.INTERFACE2MANUAL_DELIVERY.equals(toOrderType)) {
            //将接口订单转化成为人工充值订单
            order.setDeliveryProvider(DeliveryProviderEnum.MANUAL.getCode());
            manualNotify = true;
        } else if (OrderTypeEnum.isManual(omsOrder.getOrderType())
                && OrderTypeEnum.MANUAL2INTERFACE_DELIVERY.equals(toOrderType)) {
            order.setDeliveryProvider(DeliveryProviderEnum.FULU.getCode());
        }
        order.setStatus(OrderStatusEnum.WAIT_DELIVERY.getCode());
        order.setDeliveryStatus(DeliveryStatusEnum.WAIT_DELIVERY.getCode());
        if (deliveryStatusResp != null) {
            order.setDeliverySn(deliveryStatusResp.getDeliverySn());
            order.setDeliveryRemark(deliveryStatusResp.getMsg());
            order.setNote(makeNote(toOrderType.getDesc(), deliveryStatusResp.getMsg()));
        }
        order.setModifyTime(LocalDateTime.now());
        int result = orderMapper.updateById(order);
        orderManager.saveOrderOperationLog(omsOrder, operateId, operateTypeEnum, null, null, null);
        if (manualNotify) {
            OrderNotifyUtils.manualOrderNotify(omsOrder);
        }
        return result;
    }


    private int paying(PaymentResp paymentResp, OmsOrderDetail orderDetail, String payOrderSn) {
        //修改订单支付状态
        OmsOrder order = new OmsOrder();
        order.setId(orderDetail.getId());
        order.setPayOrderSn(payOrderSn);
        order.setPayStatus(PayStatusEnum.PAYING.getCode());
        order.setStatus(OrderStatusEnum.WAIT_PAY.getCode());
        order.setPaymentTime(paymentResp.getApplyTime());
        order.setPayRemark(paymentResp.getPayRemark());
        return orderMapper.updateById(order);
    }

    /** 订单支付成功 */
    public int paySuccess(PaymentResp paymentResp, OmsOrderDetail orderDetail, String payOrderSn) {
        //修改订单支付状态
        OmsOrder order = new OmsOrder();
        order.setId(orderDetail.getId());
        order.setPayOrderSn(payOrderSn);
        order.setPayStatus(PayStatusEnum.SUCCESS.getCode());
        order.setStatus(OrderStatusEnum.WAIT_DELIVERY.getCode());
        order.setPaymentTime(paymentResp.getApplyTime() == null ? orderDetail.getPaymentTime() == null ? LocalDateTime.now() : orderDetail.getPaymentTime() : paymentResp.getApplyTime());
        order.setPaymentConfirmTime(paymentResp.getApplyConfirmTime());
        order.setPayRemark(paymentResp.getPayRemark());
        orderMapper.updateById(order);
        //恢复所有下单商品的锁定库存，扣减真实库存
        return portalOrderDao.updateSkuStock(orderDetail.getOrderItemList());
    }


    public int payFail(OmsOrderDetail orderDetail, String payOrderSn, PaymentResp paymentResp) {
        //修改订单状态为取消
        OmsOrder order = new OmsOrder();
        order.setId(orderDetail.getId());
        order.setPayOrderSn(payOrderSn);
        order.setPayStatus(PayStatusEnum.FAIL.getCode());
        order.setStatus(OrderStatusEnum.CLOSED.getCode());
        order.setPaymentConfirmTime(paymentResp.getApplyConfirmTime());
        order.setPayRemark(paymentResp.getPayRemark());
        order.setNote(paymentResp.getPayRemark());
        int result = orderMapper.updateById(order);
        List<OmsOrderItem> orderItemList = orderDetail.getOrderItemList();
        //解除订单商品库存锁定
        if (!CollectionUtils.isEmpty(orderItemList)) {
            portalOrderDao.releaseSkuStockLock(orderItemList);
        }
        return result;
    }

    private int refunding(PaymentResp paymentResp, OmsOrderDetail orderDetail, String payOrderSn) {
        //修改订单支付状态
        OmsOrder order = new OmsOrder();
        order.setId(orderDetail.getId());
        order.setRefundSn(payOrderSn);
        order.setPayStatus(PayStatusEnum.REFUNDING.getCode());
        order.setStatus(OrderStatusEnum.REFUND.getCode());
        order.setRefundTime(paymentResp.getApplyTime());
        order.setPayRemark(paymentResp.getPayRemark());
        order.setNote(paymentResp.getPayRemark());
        order.setRefundReason(paymentResp.getRefundReason());
        order.setRefundAmount(orderDetail.getPayAmount());
        return orderMapper.updateById(order);
    }

    /** 订单支付成功 */
    public int refundSuccess(PaymentResp paymentResp, OmsOrderDetail orderDetail, String payOrderSn) {
        //修改订单支付状态
        OmsOrder order = new OmsOrder();
        order.setId(orderDetail.getId());
        order.setPayStatus(PayStatusEnum.REFUND.getCode());
        order.setStatus(OrderStatusEnum.REFUND.getCode());
        order.setRefundConfirmTime(paymentResp.getApplyConfirmTime());
        order.setPayRemark(paymentResp.getPayRemark());
        order.setNote(paymentResp.getPayRemark());
        return orderMapper.updateById(order);
    }


    public int refundFail(OmsOrderDetail orderDetail, String payOrderSn, PaymentResp paymentResp) {
        //修改订单状态为取消
        OmsOrder order = new OmsOrder();
        order.setId(orderDetail.getId());
        order.setPayOrderSn(payOrderSn);
        order.setPayStatus(PayStatusEnum.REFUND_FAIL.getCode());
        order.setStatus(OrderStatusEnum.FINISHED.getCode());
        order.setRefundConfirmTime(paymentResp.getApplyConfirmTime());
        order.setPayRemark(paymentResp.getPayRemark());
        order.setNote(paymentResp.getPayRemark());
        return orderMapper.updateById(order);
    }


    public Long statOrderNum() {
        if (isEnableStatConfig) {
            return orderNum;
        }
        OmsOrderExample example = new OmsOrderExample();
        example.createCriteria().andStatusEqualTo(OrderStatusEnum.FINISHED.getCode());
        example.createCriteria().andDeleteStatusEqualTo(MagicNumConstant.ZERO);
        return orderMapper.countByExample(example);
    }

    public Long statUserNum() {
        if (isEnableStatConfig) {
            return userNum;
        }
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andStatusEqualTo(MagicNumConstant.ONE);
        return umsMemberMapper.countByExample(example);
    }


    public List<UserTranInfoDto> getUserTransInfo() {
        String key = "user:trans:info";
        List<UserTranInfoDto> result;
        result = (List<UserTranInfoDto>) redisService.get(key);
        if (CollectionUtils.isEmpty(result)) {
            result = portalOrderDao.getUserTransInfo();
            if (CollectionUtils.isEmpty(result) || result.size() < 30) {
                //创造模拟数据
                int count = 0;
                for (int i = 0; i < 30 - result.size(); i++) {

                    UserTranInfoDto userTranInfoDto = new UserTranInfoDto();
                    userTranInfoDto.setBeforeMinute(BEFORE_MINUTE_STR[count]);
                    userTranInfoDto.setPayAmount(PRODUCT_AMOUNT[count]);
                    userTranInfoDto.setReceiverPhone(PHONE_STR[count]);
                    userTranInfoDto.setProductName(PRODUCT_NAME);
                    userTranInfoDto.setPaymentTime(new Date());
                    result.add(userTranInfoDto);
                    count++;
                    if (count >= BEFORE_MINUTE_STR.length) {
                        count = 0;
                    }
                }
            }
            redisService.set(key, result, 30 * 60);
        }
        return result;
    }


    /***
     * 记录接口操作日志
     */

    public void saveOrderOperationLog(OmsOrder omsOrder, Long operateId, OperateTypeEnum operateTypeEnum, String operateName, String requestInfo, String respInfo) {
        OmsOrder newOrder = orderMapper.selectById(omsOrder.getId());
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(omsOrder.getId());
        history.setOrderSn(omsOrder.getOrderSn());
        history.setOperateType(operateTypeEnum.getType());
        history.setOperateId(operateId);
        history.setOperateName(operateName);
        history.setFromOrderStatus(omsOrder.getStatus());
        history.setToOrderStatus(newOrder.getStatus());
        history.setFromPayStatus(omsOrder.getPayStatus());
        history.setToPayStatus(newOrder.getPayStatus());
        history.setFromDeliveryStatus(omsOrder.getDeliveryStatus());
        history.setToDeliveryStatus(newOrder.getDeliveryStatus());
        history.setFromOrderType(omsOrder.getOrderType());
        history.setToOrderType(newOrder.getOrderType());
        history.setPayType(newOrder.getPayType());
        history.setPayProvider(newOrder.getPayProvider());
        history.setPayOrderSn(newOrder.getPayOrderSn());
        history.setDeliveryProvider(newOrder.getDeliveryProvider());
        history.setDeliverySn(newOrder.getDeliverySn());
        history.setRequestInfo(requestInfo);
        history.setRespInfo(respInfo);
        history.setNote(newOrder.getNote());
        orderOperateHistoryMapper.insert(history);
    }

    public String makeNote(String... params) {
        if (params == null || params.length == 0) {
            return "";
        }
        return Joiner.on(":").skipNulls().join(params);
    }
}
